<?php
/**
 * PDF reporting library
 *
 * This library contains all methods needed to output
 * a PDF listing report.
 * @package Reports
 */

/**
 *
 */
require_once(AD_SERVER_PATH . '/pkgs/pdf.php');
require_once(AD_SERVER_PATH . '/reports/pdf_report.php');

define('MAX_CELL_WIDTH', 35);
define('MAX_CELL_HEIGHT', 5);
define('MAX_IMG_WIDTH', 70);
define('MAX_IMG_HEIGHT', 50);
define('MAX_URAR_STR_LEN', 28);

/**
 * PDF reporting library
 *
 * This library contains all methods needed to output
 * a PDF listing report.
 * @package Reporting
 */
class CPDF_listing extends CPDF_report
{
	/**
	 * Constructor
	 *
	 * Initilizes class variables containing
	 * path to PDF template and a FPDF object.
	 * @param string $tp_file
	 */
	public function __construct($tp_file)
	{
		$this->tp = $tp_file;
		$this->pdf = new CPDF();
	}

	/**
	 * Get PDF listing report
	 *
	 * Output PDF listing report.
	 */
	public function get_report()
	{
		$xml = simplexml_load_string($this->get_data());

		$tp = $this->_get_template($this->tp);
		$tp_rows = count($tp);

		foreach ($xml->rows->row AS $row)
		{
			$this->pdf->set_left_header_text($this->get_address($row));
			$this->pdf->set_right_header_text('MLS: ' . $row->ml_number);
			$this->pdf->AddPage();

			for ($i = 0; $i < $tp_rows; $i++)
			{
				switch ($tp[$i][0])
				{
					case 'header':
					case 'desc':
						$this->set_cords($tp[$i][1], $tp[$i][2]);
						$this->_print_header_cell($tp[$i][3], $tp[$i][4], $tp[$i][5], $tp[$i][6], $tp[$i][7], $tp[$i][8]);
						break;

					case 'field':
						$this->set_cords($tp[$i][1], $tp[$i][2]);
						$this->_print_data_cell($tp[$i][3], $tp[$i][4], $tp[$i][5], $tp[$i][6], $tp[$i][7], $tp[$i][8], $row);
						break;

					case 'img':
						$this->_print_img($row->ml_number, $tp[$i][1], $tp[$i][2]);
						break;

					default:
						break;
				}
			}

			$this->set_cords(100, 25);
			foreach (array('appliances', 'area', 'other_buildings', 'sub_division', 'directions', 'comments') AS $val)
			{
				$this->pdf->Cell(30, 5, ucfirst($val), 'T');
				$this->pdf->MultiCell(66, 5, $row->$val, 'T');
				$this->set_cords(100, '');
			}
			$this->pdf->Line(100, $this->pdf->GetY(), 196, $this->pdf->GetY());
		}
		$this->pdf->Output('AD Listing Report.pdf', 'i');
	}

	/**
	 * Print header cell
	 *
	 * Outputs header cell to PDF page.
	 * @param string $data
	 * @param int $width
	 * @param int $height
	 * @param string $border
	 * @param bool $color
	 * @param bool $newline
	 */
	private function _print_header_cell($data, $width, $height, $border, $color, $newline)
	{
		$width 	= (is_numeric($width)) 	? $width	: MAX_CELL_WIDTH;
		$height	= (is_numeric($height))	? $height	: MAX_CELL_HEIGHT;

		$this->pdf->Cell($width, $height, $data, $border, $newline, '', $color);
	}

	/**
	 * Print data cell
	 *
	 * Outputs data cell to PDF page.
	 * @param string $data
	 * @param int $width
	 * @param int $height
	 * @param string $border
	 * @param bool $color
	 * @param bool $newline
	 * @param array $xml_row
	 */
	private function _print_data_cell($data, $width, $height, $border, $color, $newline, $xml_row)
	{
		$width 	= (is_numeric($width)) 	? $width	: MAX_CELL_WIDTH;
		$height	= (is_numeric($height))	? $height	: MAX_CELL_HEIGHT;

		if  (strpos($data, '!DOLLAR!') !== FALSE)
		{
			$data = str_replace('!DOLLAR!', '', $data);
			$is_dollar = TRUE;
		}
		elseif  (strpos($data, '!DATE!') !== FALSE)
		{
			$data = str_replace('!DATE!', '', $data);
			$is_date = TRUE;
		}

		
		if (strpos($data, ' ') !== FALSE)
		{
			$string = $this->_parse_multiple_fields($data, $xml_row);
		}
		elseif (strpos($data, '.') !== FALSE)
		{
			$string = $this->_concat_fields($data, $xml_row);
		}
		elseif (strpos($data, '+') !== FALSE)
		{
			$string = $this->_add_fields($data, $xml_row);
		}
		elseif (strpos($data, '/') !== FALSE)
		{
			$string = $this->_divide_fields($data, $xml_row);
		}
		else //
		{
			$string = $this->_get_field($data, $xml_row);
		}

		
		
		if (isset($is_dollar))
		{
			$string = ($string != '') ? '$' . number_format($string, 0) : '';
		}
		elseif (isset($is_date))
		{
			$string = date('m/d/Y', strtotime($string));
		}
		
		
		if (strlen($string) > MAX_URAR_STR_LEN)
		{
			$string = substr_replace($string, '...', (MAX_URAR_STR_LEN -3));
		}
		$this->pdf->Cell($width, $height, $string, $border, $newline, '', $color);
	}

	/**
	 * Print image
	 *
	 * Outputs image to PDF page.
	 * @param int $ml_number
	 * @param int $x
	 * @param int $y
	 */
	private function _print_img($ml_number, $x, $y)
	{
		$this->pdf->Image('', $x, $y, MAX_IMG_WIDTH, MAX_IMG_HEIGHT, '', '', $ml_number, 0);
	}

	/**
	 * Get template
	 *
	 * Reads in line from template and appends it to array.
	 * @param string $tp_file
	 * @return array $field
	 */
	private function _get_template($tp)
	{
		$field = array();

		$fh = fopen($tp, 'r');
		while (($line = fgetcsv($fh, 250, ',')) !== FALSE)
		{
			array_push($field, $line); //$field[] = $line;
		}
		fclose($fh);
		
		return $field;
	}

	/**
	 * Get field
	 *
	 * Gets data from XML and returns it.
	 * @param string $string
	 * @param array $xml_row
	 * @return string $output
	 */
	private function _get_field($string, $xml_row)
	{
		$output = (string)$xml_row->$string;
		return $output;
	}

	/**
	 * Parse multiple fields
	 *
	 * Parses data for cell that are separated by spaces.
	 * @param string $string
	 * @param array $xml_row
	 * @return string $output
	 */
	private function _parse_multiple_fields($string, $xml_row)
	{
		$output = '';
		$value = explode(' ', $string);
		foreach ($value AS $val)
		{
			$output .= (strlen($val) > 1) ? ($xml_row->$val . ' ') : $val;
		}
		return $output;
	}

	/**
	 * Concat fields
	 *
	 * Concats all fields contained in cell.
	 * @param string $string
	 * @param array $xml_row
	 * @return string $output
	 */
	private function _concat_fields($string, $xml_row)
	{
		$output = '';
		$value = explode('.', $string);
		foreach ($value AS $val)
		{
			$output .= $xml_row->$val . '.';
		}
		$output = substr_replace($output, '', - 1);
		return $output;
	}

	/**
	 * Add fields
	 *
	 * Adds all fields contained in cell.
	 * @param string $string
	 * @param array $xml_row
	 * @return float $total
	 */
	private function _add_fields($string, $xml_row)
	{
		$total = 0;
		$number = explode('+', $string);
		foreach ($number AS $num)
		{
			$total += $xml_row->$num;
		}
		return $total;
	}

	/**
	 * Divide fields
	 *
	 * Divide all fields contained in cell.
	 * @param string $string
	 * @param array $xml_row
	 * @return float $total
	 */
	private function _divide_fields($string, $xml_row)
	{
		$total = 0;
		$number = explode('/', $string);
		foreach ($number AS $num)
		{
			if ($xml_row->$num > 0)
			{
				$total = ($total == 0) ? $xml_row->$num : ($total / $xml_row->$num);
			}
		}
		return floor($total);
	}
}
